package com.service.Impl;

import com.entity.Book;
import com.entity.RequestParam;
import com.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.service.BookService;
import utils.Result;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public Result  queryBooks(RequestParam req) {
        Result result = new Result();
        req.setCurrent(((req.getCurrent())-1) * req.getSize());
        List<Book> books = bookMapper.queryBooks(req);
        result.setData(books);
        Integer count = bookMapper.queryBooksNum(req);
        result.setCount(count);
        return result;
    }

    @Override
    public Book queryBookOne(Integer id) {
        return bookMapper.queryOne(id);
    }

    @Override
    public Integer deleteBookOne(Integer id) {
        return bookMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(Book book) {
         return bookMapper.updateByPrimaryKey(book);
    }

    @Override
    public Integer save(Book book) {
        return bookMapper.insertSelective(book);
    }

    @Override
    public void importBook(List<Book> books) {
        for (Book book : books) {
            bookMapper.insertSelective(book);
        }
    }
}
